package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.common.ResultMsg;
import com.tbit.uqbike.client.convert.OrderTraceIdApplyConverter;
import com.tbit.uqbike.client.dto.*;
import com.tbit.uqbike.client.request.EventReportReq;
import com.tbit.uqbike.client.request.OrderTraceIdApplyReq;
import com.tbit.uqbike.client.service.ordertrace.OrderTraceService;
import com.tbit.uqbike.config.mvc.CommonHeader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 事件控制器
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/7/2
 */
@RestController
@Slf4j
@RequestMapping("/event")
public class OrderTraceController {

    @Autowired
    private OrderTraceService orderTraceService;


    /**
     * 申请订单跟踪号
     *
     * @param req 订单跟踪号申请请求
     * @return 订单跟踪号
     */
    @PostMapping("/applyTraceId")
    public ResultMsg<String> applyTraceId(@RequestBody @Valid OrderTraceIdApplyReq req) {
        OrderTraceIdApplyDTO apply = OrderTraceIdApplyConverter.toDTO(req);
        String traceId = orderTraceService.applyOrderTraceId(apply);
        return ResultMsg.success(traceId);
    }

    /**
     * 事件上报
     *
     * @param req 事件上报请求
     * @return 事件上报结果
     */
    @PostMapping("/report")
    public ResultMsg<Void> report(@RequestBody @Valid EventReportReq req) {
        EventReportDTO report = OrderTraceIdApplyConverter.toDTO(req);
        orderTraceService.reportEvent(report);
        return ResultMsg.success();
    }

    /**
     * 事件上报 - 新
     *
     * @param req 事件上报请求
     * @return 事件上报结果
     */
    @PostMapping("/put")
    public ResultMsg<Void> put(@RequestBody @Valid EventPutReqDTO req , @CommonHeader CommonHeaderDTO headers) {
        EventPutDTO eventPutDTO = EventPutDTO.getInstance(req,  headers);
        orderTraceService.putEvent(eventPutDTO);
        return ResultMsg.success();
    }

}
